package io.renren.modules.biz.controller;

import io.renren.common.annotation.SysLog;
import io.renren.common.utils.R;
import io.renren.common.validator.Assert;
import io.renren.modules.biz.entity.MemberConfigEntity;
import io.renren.modules.biz.service.MemberConfigService;

import java.util.Date;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 会员中奖踩雷配置表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-09-03 14:51:31
 */
@RestController
@RequestMapping("biz/memberconfig")
public class MemberConfigController {
	@Autowired
	private MemberConfigService memberConfigService;

	/**
	 * 信息
	 */
	@RequestMapping("/info/{memberId}")
	@RequiresPermissions("biz:memberconfig:info")
	public R info(@PathVariable("memberId") Long memberId) {
		MemberConfigEntity memberConfig = memberConfigService.selectById(memberId);

		return R.ok().put("memberConfig", memberConfig);
	}

	/**
	 * 会员配置
	 */
	@SysLog("会员配置")
	@RequestMapping("/update")
	@RequiresPermissions("biz:memberconfig:update")
	public R update(@RequestBody MemberConfigEntity memberConfig) {
		if (memberConfig.getLandmineRateMax() != null) {
			Assert.isTrue(memberConfig.getLandmineRateMax() >= 0 && memberConfig.getLandmineRateMax() <= 1, "最大踩雷概率必须在0-1之间");
		}
		if (memberConfig.getProfitLimit() != null && memberConfig.getLossLimit() != null) {
			Assert.isTrue(memberConfig.getProfitLimit() > memberConfig.getLossLimit(), "最大盈利必须大于最大亏损");
		}
		if (memberConfig.getPointTime() == null) {
			memberConfig.setPointTime(new Date());
		}
		memberConfigService.insertOrUpdateAllColumn(memberConfig);
		return R.ok();
	}
}
